// packageA/pages/org-detail/index.js
const WXAPI = require("../../api/org");
const WXAPI2 = require("../../api/api");
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
import { handleResult } from "../../utils/tools";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    longitude: "", //经度
    latitude: "", //维度
    scale: 13, //地图默认缩放等级
    markers: [], //点位数据
    activeClick: false,
    currentPreviewImg: "",
    isCollect: false,
    showImage: false,
    orgInfo: {},
    evaluateInfo: {},
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    currentId: "",
    orgId: "",
    background: ["demo-text-1", "demo-text-2", "demo-text-3"],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
  },
  toOrderService() {
    let data = {
      orgName: this.data.orgInfo.sServiceOrgName,
      tips: this.data.orgInfo.sOrgFeature,
      dMinFee: this.data.orgInfo.dMinFee,
      dMaxFee: this.data.orgInfo.dMaxFee,
      address: this.data.orgInfo.sOrgAddress,
      imgUrl: this.data.imgBaseUrl + this.data.orgInfo.sDoorheadPhoto,
      id: this.data.orgInfo.sId,
      orgId: this.data.orgInfo.sOrgId,
    };
    wx.navigateTo({
      url: `/packageA/pages/org-appointment/index?orgObj=${JSON.stringify(
        data
      )}`,
    });
  },
  onClose() {
    this.setData({
      showImage: !this.data.showImage,
    });
  },
  async collect() {
    // 点击动画
    this.setData({
      activeClick: true,
    });
    setTimeout(() => {
      this.setData({
        activeClick: false,
      });
    }, 500);

    if (this.data.isCollect) {
      let res = await WXAPI.cancelCollect(this.data.orgId);
      if (res.data.code == 0) {
        this.setData({
          isCollect: false,
        });
        wx.showToast({
          title: res.data.message,
        });
      }
    } else {
      let res = await WXAPI.sureCollect(this.data.orgId);
      if (res.data.code == 0) {
        this.setData({
          isCollect: true,
        });
        wx.showToast({
          title: res.data.message,
        });
      }
    }
  },
  view(e) {
    this.setData({
      currentPreviewImg: e.currentTarget.dataset.url,
      showImage: true,
    });
  },
  async getImagesList(id) {
    let res = await WXAPI2.getFiles({
      sBusinessId: this.data.currentId,
      sBusinessType: "Org_Introduce",
      iFileFullUrl: 1,
    });
    if (res.data.code == 0) {
      this.setData({
        imgList: res.data.data,
      });
    }
  },
  async getDetail() {
    let res = await WXAPI.getOrgDetail(this.data.currentId);
    if (res.data.code == 0) {
      this.getImagesList();
      // res.data.data.sPublicityContent = unescape(res.data.data.sPublicityContent)
      this.setData({
        orgInfo: handleResult(res.data.data),
        isCollect: res.data.data.iCollect == 0 ? false : true,
      });
      let marker = [
        {
          id: 1,
          longitude: res.data.data.dLng,
          latitude: res.data.data.dLat,
          iconPath: "../org/position.png",
          width: 31,
          height: 40,
          label: {
            fontSize: 12,
            // borderWidth: 1,
            padding: 6,
            borderRadius: 15,
            bgColor: "#ffffff",
            textAlign: "center",
            anchorX: -((res.data.data.sServiceOrgName.length / 2) * 12),
            anchorY: -75,
            content: res.data.data.sServiceOrgName,
          },
        },
      ];
      this.setData({
        markers: marker,
        longitude: res.data.data.dLng,
        latitude: res.data.data.dLat,
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 初始化地图
    this.mapCtx = wx.createMapContext("mapId");
    // 存储id
    this.setData({
      currentId: options.id,
      orgId: options.orgId,
    });
    wx.setNavigationBarTitle({
      title: options.title,
    });
    // 详情
    this.getDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
